// pages/inferioraList/inferioraAddress.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: "",
    orderId: '',
    goodsId: '',
    showAddress: null,
    address: '',
    custId: '',
    code: '',
    mobile: '',
    name: '',
    id: '',
    title: '',
    isDefault: 0,
    canSubmit:true, 
    region:['请选择省、市、区县'],
    isTakeBySelf:false,
  },
  changeTake(e){
    console.log(e);
  
      this.setData({
        isTakeBySelf:!e.currentTarget.dataset.item
      })
    
  },
  navToAddrList(e) {
    wx.navigateTo({
      url: '/pages/inferioraList/inferioraList?canOpen=1',
    })
  },
  bindRegionChange(e){
    this.setData({
      region:e.detail.value
    })
  },
  bindSave(e) {
    let that = this
    // console.log(this.data.selCity, this.data.selDistrict, this.data.selProvince)
    var form = e.detail.value;

    let AddressJson = {};
    if (form.linkMan == "") {
      wx.showToast({
        icon: "error",
        title: '请填写收货人',
      })
      return;
    }
    if (form.mobile == "") {
      wx.showToast({
        icon: "error",
        title: '请填写联系电话',
      })
      return;
    }
    if(this.data.region.length<=0){
      wx.showModal({
        title: '提示',
        content: '请选择地址',
        showCancel: false
      })
      return
    }
    if(!this.data.isTakeBySelf){
      if (this.data.region[0].indexOf('请选择')>=0) {
        wx.showModal({
          title: '提示',
          content: '请选择省',
          showCancel: false
        })
        return
      }
      if (this.data.region[1].indexOf('请选择')>=0) {
        wx.showModal({
          title: '提示',
          content: '请选择市',
          showCancel: false
        })
        return
      }
      if (this.data.region[2].indexOf('请选择')>=0) {
        wx.showModal({
          title: '提示',
          content: '请选择区县',
          showCancel: false
      })
      return
    }
    if (form.address == "") {
      wx.showToast({
        icon: "error",
        title: '请填写收货地址',
      })
      return;
    }
    
  AddressJson = {
      "Receive": this.data.name,
      "Tel": this.data.mobile,
      "Detail": this.data.address,
      "Province": this.data.region[0],
      "City": this.data.region[1],
      "District": this.data.region[2]
    };

  }else{
    AddressJson = {
      "Receive": this.data.name,
      "Tel": this.data.mobile,
      "Detail": "自提",
      "Province": "",
      "City": "",
      "District": "",
    };
  }
    let ShoppingCartData = [{
      "CustId": 0,
      "GoodsId": this.data.goods.GoodsId,
      "GoodsName": this.data.goods.GoodsName,
      "Num": this.data.goods.buyNum * 6,
      // "Price": this.data.goods.Price,
      "StandardId": this.data.goods.StandardId,
      "StandardName": this.data.goods.StandardName,
      "Thumb": this.data.goods.Thumb,
      "GoodsLibId": this.data.goods.GoodsLibId
    }]
    let OrderId = this.data.orderId;
    let DealerCustId = app.globalData.uid;
    let CustId = this.data.custId;
    console.log(CustId, OrderId, DealerCustId, AddressJson, ShoppingCartData);
    wx.showLoading({
      title: '提交中',
      mask: true
    })
    app.postForm("dealerorder/test_insert_dealer_order", {
      CustId,
      OrderId,
      AddressJson: JSON.stringify(AddressJson),
      ShoppingCartData: JSON.stringify(ShoppingCartData),
      DealerCustId,
      Submitter:app.globalData.uid
    }, res => {
      wx.hideLoading();
      if (res && res.code == "0") {
        this.data.goods.stock = Number(this.data.goods.stock) - Number(this.data.goods.buyNum);
        this.setData({
          goods: this.data.goods,
          canSubmit:false
        })
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.onShow();
        wx.showModal({
          content: "提货单以提交，请等待发货",
          showCancel: false,
          success (res) {
            if (res.confirm) {
              wx.navigateBack();
            }
          }
        })
      } else {
        wx.showModal({
          content: res.msg,
          showCancel: false
        })
      }
    })


  },
  selectAddress: function () {
    wx.navigateTo({
      url: "/pages/address/address"
    })
  },
  
  selectTap(e) {
    this.setData({
      showAddress: e.currentTarget.dataset.item
    })
    let add = this.data.showAddress
    console.log(add)
    let address = `${add.Province}${add.City}${add.District == '请选择' ? '' : add.District}${add.Detail}`
    this.setData({
      address
    })
  },


  jianBtnTap(e) {
    if (this.data.goods.buyNum == 1) {
      return;
    }
    this.data.goods.buyNum = Number(this.data.goods.buyNum) - 1;
    this.setData({
      goods: this.data.goods
    })
  },  
  jiaBtnTap(e) {
    if (this.data.goods.buyNum * 6 + 6 > this.data.goods.Num) {
      return;
    }
    this.data.goods.buyNum = Number(this.data.goods.buyNum) + 1;
    this.setData({
      goods: this.data.goods
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let goods = wx.getStorageSync('stock-goods');
    this.getAddress = this.selectComponent("#getAddress");
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowWidth: res.windowWidth, //可使用窗口宽度
          windowHeight: res.windowHeight, //可使用窗口高度
        })
      }
    })
    app.get('user/get_address_by_custid?CustId=' + app.globalData.uid, res => {
      console.log(res)
      if (res.code == 0) {
        if(res.result.length>0){

          var data=res.result[0];
          that.setData({
            region: [data.Province, data.City, data.District],   
            address: data.Detail,
            name: data.Receive,
            mobile: data.Tel
          });
        }
      }
    })
    wx.hideShareMenu()
    if (goods) {
      console.log(goods)
      goods.buyNum = 1;
      this.setData({
        // goodsId: options.id,
        // orderId: options.orderId,
        goods: goods
      })
    } else {
      wx.navigateBack();
    }
     app.get('user/get_address_by_custid?CustId=' + app.globalData.uid, res => {
      console.log(res)
      if (res.code == 0) {
        if(res.result.length>0){
          var data=res.result[0];          
          that.setData({
            region:[data.Province, data.City, data.District],
            address: data.Detail,
            name: data.Receive,
            mobile: data.Tel
          });
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})